<?php

namespace App\Http\Controllers\Admin;


use App\Models\AuthGroup;
use App\Models\Service;
use App\Models\ServiceCharge;

class ServiceController extends BaseController
{


    public function __construct(Service $service, ServiceCharge $serviceCharge, AuthGroup $authGroup)
    {
        parent::__construct();
        $this->service = $service;
        $this->serviceCharge = $serviceCharge;
        $this->authGroup = $authGroup;
    }

    public function getIndex()
    {
        $rows = $this->service->_getList();
        $groups = $this->authGroup->_getList();
        return view('admin.service.index', compact('rows', 'groups'));
    }

    public function getRecord()
    {
        $rows = $this->serviceCharge->_getList();
        return view('admin.service.record', compact('rows'));
    }


    public function getAdd()
    {
        $groups = $this->authGroup->_getList();
        return view('admin.service.edit', compact('groups'));
    }

    public function postAdd()
    {
        $res = $this->service->addOrUpdate();
        if (!$res) {
            return $this->error($this->service->getError() ? $this->service->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('service', 'getIndex'));
    }

    public function getEdit()
    {
        $groups = $this->authGroup->_getList();
        $info = $this->service->_getOneById(request('id'));
        return view('admin.service.edit', compact('info', 'groups'));
    }

    public function postEdit()
    {
        $res = $this->service->addOrUpdate();
        if (!$res) {
            return $this->error($this->service->getError() ? $this->service->getError() : '未知错误');
        }
        return $this->success('更新成功');
    }

}
